import emitter from "@/utils/emitter.js";

class clickEvent {
    constructor(graph, optionsConfig) {
        this.graph = graph
        this.optionsConfig = optionsConfig
        this.clickTimeout = null
        this.clickDelay = 250
    }

    init() {
        // 节点点击事件
        this.graph.on("node:click", (event) => {
            if (this.clickTimeout) {
                clearTimeout(this.clickTimeout);
                this.clickTimeout = null;
                return;
            }
            this.clickTimeout = setTimeout(() => {
                // 单击事件开始------------------------------------------------------------------------
                const data = event.target.attributes.originalProperties
                const params = {data, signal: 'clickNode'}
                // 向父页面发送消息
                window.parent.postMessage(params, '*');
                // 单击事件结束------------------------------------------------------------------------
                this.clickTimeout = null;
            }, this.clickDelay);

        })
        // 节点双击事件
        this.graph.on("node:dblclick", (event) => {
            // 防抖开始-----------------------------------------------
            if (this.clickTimeout) {
                clearTimeout(this.clickTimeout);
                this.clickTimeout = null;
            }
            // 防抖结束----------------------------------------------
            const data = event.target.attributes.originalProperties
            const params = {data, signal: 'nodeData'}
            emitter.emit('refreshData', params)
        })
        // 画布单击事件
        this.graph.on("canvas:click", async (event) => {
        })
        // 画布双击事件
        this.graph.on("canvas:dblclick", async (event) => {

        })
    }
}

export default clickEvent;
